using System;
using Gtk;

namespace Sqlpager
{
	public class ComboDate : HBox
    {
        HBox mBox;
        EventBox mEventBox;
        ToggleButton mButton;
        Window mWindow;
        DateTime mDate; 
        Calendar mCalendar;
        Entry mDateEntry;

        public DateTime Date
        {
            get { return mDate; }
            set { 
				mDate = value; 
				mDateEntry.Text = mDate.ToString("MM/dd/yyyy");
			}
        }
		public string Text
		{
			get{return mDateEntry.Text;}	
		}
		public ComboDate()
        {
            mBox = new HBox();
            HBox hbox = new HBox(false, 0);
            mEventBox = new EventBox();
            mButton = new ToggleButton();
            mButton.Toggled += OnButtonToggled;
            Arrow arrow = 
				new Arrow(ArrowType.Down, ShadowType.None);
            mDateEntry = new Entry();
            hbox.PackStart(mDateEntry, false, false, 0);
            hbox.PackStart(mButton, false, false, 0);
            mButton.Add(arrow);
            mEventBox.Add(hbox);
            mBox.PackStart(mEventBox, true, true, 0);
            PackStart(mBox, false, false, 0);
            mWindow = new Gtk.Window(Gtk.WindowType.Popup);
            mCalendar = new Calendar();
            mCalendar.KeyPressEvent += 
				new Gtk.KeyPressEventHandler(OnKeyPress);
            mCalendar.ButtonPressEvent += 
				new Gtk.ButtonPressEventHandler(OnButtonClick);
            mWindow.Add(mCalendar);
        }

        private void OnButtonToggled(object o, EventArgs e)
        {
			Console.WriteLine(mButton.Active.ToString());
            if (mButton.Active)
                ShowCalendar();
            else
                HideCalendar();
        }

        private void ShowCalendar()
        {
            mCalendar.Date = mDate;
            int x, y, width, height, depth;
            mEventBox.GdkWindow.GetGeometry(
			    out x, out y, out width, out height, out depth);
            mEventBox.GdkWindow.GetOrigin(out x, out y);

            mWindow.Move(x, y + height);
            mWindow.ShowAll();
			
            // Grab mouse and keyboard events
            Gdk.Pointer.Grab(mWindow.GdkWindow, true,
                Gdk.EventMask.ButtonPressMask | 
			    Gdk.EventMask.ButtonReleaseMask | 
			    Gdk.EventMask.PointerMotionMask | 
			    Gdk.EventMask.EnterNotifyMask | 
			    Gdk.EventMask.LeaveNotifyMask,
                null, null, Gtk.Global.CurrentEventTime);
            Gdk.Keyboard.Grab(mWindow.GdkWindow, true, 
				Gtk.Global.CurrentEventTime);
            Gtk.Grab.Add(mWindow);
            
        }

        private void OnKeyPress(object o, Gtk.KeyPressEventArgs args)
        {
            if (args.Event.Key == Gdk.Key.Return || 
			    args.Event.Key == Gdk.Key.Escape)
                mButton.Active = false;
        }

        private void OnButtonClick(
			object o, Gtk.ButtonPressEventArgs args)
        {
            if (args.Event.Type == Gdk.EventType.TwoButtonPress)
                mButton.Active = false;
        }

        private void HideCalendar()
        {
            Gtk.Grab.Remove(mWindow);
            Gdk.Keyboard.Ungrab(Gtk.Global.CurrentEventTime);
            Gdk.Pointer.Ungrab(Gtk.Global.CurrentEventTime);

            // mButton.Active = false;
			mWindow.Hide();

            if (mDate == mCalendar.Date)
                return;

            mDate = mCalendar.Date;
            mDateEntry.Text = mDate.ToString("MM/dd/yyyy");

            //if (DateChanged != null)
                //DateChanged(mDate);
        }
   		public new void GrabFocus()
		{
			mDateEntry.GrabFocus();
		}
		
    }
}
